{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-05-23T03:18:07.002607Z",
     "start_time": "2025-05-23T03:18:03.890158Z"
    }
   },
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import numpy as np\n",
    "\n",
    "class LinearModel(nn.Module):\n",
    "    def __init__(self, embedding_file='embedding_SougouNews.npz', class_num=10):\n",
    "        super(LinearModel, self).__init__()\n",
    "        self.embedding_size = 300\n",
    "        # 加载预训练词向量\n",
    "        embedding_pretrained = torch.tensor(\n",
    "            np.load(embedding_file)[\"embeddings\"].astype('float32'))\n",
    "        self.embedding = nn.Embedding.from_pretrained(embedding_pretrained, freeze=False)\n",
    "        # 直接使用线性层分类\n",
    "        self.classifier = nn.Linear(self.embedding_size, class_num)\n",
    "\n",
    "    def forward(self, x):\n",
    "        # 输入：[batch_size, seq_len]\n",
    "        embedded = self.embedding(x).float()  # [batch_size, seq_len, embedding_dim]\n",
    "        # 平均池化获取句向量\n",
    "        pooled = torch.mean(embedded, dim=1)  # [batch_size, embedding_dim]\n",
    "        logits = self.classifier(pooled)\n",
    "        return logits"
   ],
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-23T03:18:35.608391Z",
     "start_time": "2025-05-23T03:18:33.652448Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA\n",
    "import numpy as np\n",
    "\n",
    "class LDAModel:\n",
    "    def __init__(self, embedding_file='embedding_SougouNews.npz'):\n",
    "        # 加载预训练词向量（仅用于获取特征维度，实际需用文本特征矩阵训练）\n",
    "        self.embedding_size = 300\n",
    "        self.model = LDA(n_components=class_num-1)  # LDA维度为类别数-1\n",
    "\n",
    "    def train(self, X_train, y_train):\n",
    "        # X_train: 文本特征矩阵（如平均词向量），形状为 [n_samples, embedding_size]\n",
    "        # y_train: 标签向量，形状为 [n_samples]\n",
    "        self.model.fit(X_train, y_train)\n",
    "\n",
    "    def predict(self, X_test):\n",
    "        # X_test: 测试集特征矩阵\n",
    "        return self.model.predict(X_test)\n",
    "\n",
    "    def predict_proba(self, X_test):\n",
    "        return self.model.predict_proba(X_test)"
   ],
   "id": "86057d50ea183ba0",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-23T03:18:48.156959Z",
     "start_time": "2025-05-23T03:18:48.142645Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.svm import SVC\n",
    "import numpy as np\n",
    "\n",
    "class SVMModel:\n",
    "    def __init__(self, embedding_file='embedding_SougouNews.npz', kernel='linear', C=1.0):\n",
    "        self.embedding_size = 300\n",
    "        self.kernel = kernel\n",
    "        self.C = C\n",
    "        self.model = SVC(kernel=kernel, C=C, probability=True)\n",
    "\n",
    "    def train(self, X_train, y_train):\n",
    "        # X_train: 文本特征矩阵（如平均词向量或TF-IDF）\n",
    "        self.model.fit(X_train, y_train)\n",
    "\n",
    "    def predict(self, X_test):\n",
    "        return self.model.predict(X_test)\n",
    "\n",
    "    def predict_proba(self, X_test):\n",
    "        return self.model.predict_proba(X_test)"
   ],
   "id": "fc0435007f64e642",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-23T03:19:04.318959Z",
     "start_time": "2025-05-23T03:19:04.297642Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "\n",
    "class TraditionalCNNModel(nn.Module):\n",
    "    def __init__(self, embedding_file='embedding_SougouNews.npz', class_num=10, kernel_sizes=[2, 3, 4]):\n",
    "        super(TraditionalCNNModel, self).__init__()\n",
    "        self.embedding_size = 300\n",
    "        # 加载预训练词向量\n",
    "        embedding_pretrained = torch.tensor(\n",
    "            np.load(embedding_file)[\"embeddings\"].astype('float32'))\n",
    "        self.embedding = nn.Embedding.from_pretrained(embedding_pretrained, freeze=False)\n",
    "        # 定义卷积层（输入通道1，输出通道256，不同核大小）\n",
    "        self.convs = nn.ModuleList([\n",
    "            nn.Conv2d(1, 256, (k, self.embedding_size)) for k in kernel_sizes\n",
    "        ])\n",
    "        self.dropout = nn.Dropout(0.3)\n",
    "        self.classifier = nn.Linear(256 * len(kernel_sizes), class_num)\n",
    "\n",
    "    def forward(self, x):\n",
    "        # 输入：[batch_size, seq_len]\n",
    "        embedded = self.embedding(x).unsqueeze(1)  # [batch_size, 1, seq_len, embedding_dim]\n",
    "        # 多尺度卷积+池化\n",
    "        conv_outputs = []\n",
    "        for conv in self.convs:\n",
    "            out = conv(embedded)  # [batch_size, 256, seq_len-k+1, 1]\n",
    "            out = F.relu(out).squeeze(3)  # [batch_size, 256, seq_len-k+1]\n",
    "            out = F.max_pool1d(out, out.size(2)).squeeze(2)  # [batch_size, 256]\n",
    "            conv_outputs.append(out)\n",
    "        # 拼接特征\n",
    "        merged = torch.cat(conv_outputs, dim=1)  # [batch_size, 256*len(kernel_sizes)]\n",
    "        merged = self.dropout(merged)\n",
    "        logits = self.classifier(merged)\n",
    "        return logits"
   ],
   "id": "f1ded3e1273e59c2",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-23T03:30:23.419349Z",
     "start_time": "2025-05-23T03:30:23.303326Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "e1e16cadfa9f6728",
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'embedding_SougouNews.npz'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mFileNotFoundError\u001B[0m                         Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[16], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mscipy\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01msparse\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m load_npz\n\u001B[1;32m----> 2\u001B[0m data_s \u001B[38;5;241m=\u001B[39m load_npz(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124membedding_SougouNews.npz\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m      3\u001B[0m data_s\u001B[38;5;241m.\u001B[39mdata\n",
      "File \u001B[1;32mD:\\python\\Lib\\site-packages\\scipy\\sparse\\_matrix_io.py:134\u001B[0m, in \u001B[0;36mload_npz\u001B[1;34m(file)\u001B[0m\n\u001B[0;32m     80\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mload_npz\u001B[39m(file):\n\u001B[0;32m     81\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\" Load a sparse array/matrix from a file using ``.npz`` format.\u001B[39;00m\n\u001B[0;32m     82\u001B[0m \n\u001B[0;32m     83\u001B[0m \u001B[38;5;124;03m    Parameters\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    132\u001B[0m \u001B[38;5;124;03m    >>> sparse_array = sp.sparse.csr_array(tmp)\u001B[39;00m\n\u001B[0;32m    133\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n\u001B[1;32m--> 134\u001B[0m     \u001B[38;5;28;01mwith\u001B[39;00m np\u001B[38;5;241m.\u001B[39mload(file, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mPICKLE_KWARGS) \u001B[38;5;28;01mas\u001B[39;00m loaded:\n\u001B[0;32m    135\u001B[0m         sparse_format \u001B[38;5;241m=\u001B[39m loaded\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mformat\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m    136\u001B[0m         \u001B[38;5;28;01mif\u001B[39;00m sparse_format \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n",
      "File \u001B[1;32mD:\\python\\Lib\\site-packages\\numpy\\lib\\npyio.py:427\u001B[0m, in \u001B[0;36mload\u001B[1;34m(file, mmap_mode, allow_pickle, fix_imports, encoding, max_header_size)\u001B[0m\n\u001B[0;32m    425\u001B[0m     own_fid \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mFalse\u001B[39;00m\n\u001B[0;32m    426\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m--> 427\u001B[0m     fid \u001B[38;5;241m=\u001B[39m stack\u001B[38;5;241m.\u001B[39menter_context(\u001B[38;5;28mopen\u001B[39m(os_fspath(file), \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrb\u001B[39m\u001B[38;5;124m\"\u001B[39m))\n\u001B[0;32m    428\u001B[0m     own_fid \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mTrue\u001B[39;00m\n\u001B[0;32m    430\u001B[0m \u001B[38;5;66;03m# Code to distinguish from NumPy binary files and pickles.\u001B[39;00m\n",
      "\u001B[1;31mFileNotFoundError\u001B[0m: [Errno 2] No such file or directory: 'embedding_SougouNews.npz'"
     ]
    }
   ],
   "execution_count": 16
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
